package mobi.weq.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import mobi.weq.db.Database;
import mobi.weq.model.User;

public class UserDao {
	public synchronized int addUser(User s) throws SQLException {
		Connection c = Database.getConnection();
		String query = "insert into user(id,username,display_name,password, email, use_user_feedback, has_lung_problems,"
				+ "has_heart_problems,age_group) values(?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = c.prepareStatement(query);
		ps.setString(1, s.getUserId());
		ps.setString(2, s.getUserName());
		ps.setString(3, s.getDisplayName());
		ps.setString(4, s.getPassword());
		ps.setString(5, s.getEmail());

		ps.setBoolean(6, s.isUsingUserFeedback());
		ps.setBoolean(7, s.isHavingLungProblems());
		ps.setBoolean(8, s.isHavingHeartProblems());
		ps.setString(9, s.getAgeGroup());

		System.out.println(ps);
		return ps.executeUpdate();
	}

	public List<User> getAllUsers() throws SQLException {
		Connection c = Database.getConnection();
		String query = "select * from user";
		PreparedStatement ps = c.prepareStatement(query);

		ResultSet rs = ps.executeQuery();
		List<User> users = new ArrayList<User>();

		while (rs.next()) {
			User us = getUserFromResultSet(rs);
			users.add(us);
		}
		return users;

	}

	public User getUserByUsernamePassword(String username, String password) throws SQLException {
		Connection c = Database.getConnection();
		String query = "select * from user where username=? and password=?";
		PreparedStatement ps = c.prepareStatement(query);
		ps.setString(1, username);
		ps.setString(2, password);

		ResultSet rs = ps.executeQuery();

		if (rs.next()) {
			User us = getUserFromResultSet(rs);
			return us;
		}
		return null;

	}

	private User getUserFromResultSet(ResultSet rs) throws SQLException {
		User u = new User();
		u.setUserId(rs.getString(1));
		u.setUserName(rs.getString(2));
		u.setDisplayName(rs.getString(3));
		u.setPassword(rs.getString(4));
		u.setEmail(rs.getString(5));

		u.setUsingUserFeedback(rs.getBoolean(6));
		u.setHavingLungProblems(rs.getBoolean(7));
		u.setHavingHeartProblems(rs.getBoolean(8));
		u.setAgeGroup(rs.getString(9));
		return u;
	}

	public synchronized int deleteUser(User s) throws SQLException {
		Connection cn = Database.getConnection();
		String sql = "delete from user where username=?";// and password=?";\
		
		PreparedStatement ps = cn.prepareStatement(sql);
		ps.setString(1, s.getUserName());
		return ps.executeUpdate();
	}

	public User updateUser(User u) throws SQLException {
//		UPDATE Customers
//		SET ContactName='Alfred Schmidt', City='Hamburg';
		Connection cn = Database.getConnection();
		String sql = "update user set use_user_feedback=?, has_lung_problems=?,	has_heart_problems=?, age_group=? where id=?";
		PreparedStatement ps = cn.prepareStatement(sql);
		ps.setBoolean(1, u.isUsingUserFeedback());
		ps.setBoolean(2, u.isHavingLungProblems());
		ps.setBoolean(3, u.isHavingHeartProblems());
		ps.setString(4, u.getAgeGroup());
		ps.setString(5, u.getUserId());
		ps.executeUpdate();

		return u;
	}

	public boolean doesUsernameExists(String username) throws SQLException{
		// TODO Auto-generated method stub
		return true;
	}
}
